<script setup lang="tsx">
import { appUserListApi } from '@/api/AppUser'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
// import { useI18n } from '@/hooks/web/useI18n'
import { useTable } from '@/hooks/web/useTable'

// const { t } = useI18n()

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const res = await appUserListApi({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data.list || [],
      total: res.data.total || 0
    }
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList } = tableMethods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: '用户ID',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'nickName',
    label: '用户昵称',
    search: {
      componentProps: {
        placeholder: '请输入用户昵称'
      }
    },
    form: {
      hidden: true
    },
    table: {
      width: 520,
      slots: {
        default: (data: any) => {
          const row = data.row as any
          return (
            <>
              <div class={'flex items-center'}>
                <img class={'w-10 mr-2'} src={row.avatar} />
                <span>{row.nickName}</span>
              </div>
            </>
          )
        }
      }
    }
  },
  {
    field: 'mobile',
    label: '手机号',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'lastLoginTime',
    label: '最近登录时间',
    search: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetimerange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'registerTime',
    label: '注册时间',
    search: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetimerange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    form: {
      hidden: true
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const searchParams = ref({})
const setSearchParams = (params: any) => {
  const { lastLoginTime, registerTime, ...rest } = params
  let startLastLoginTime, endLastLoginTime, startRegisterTime, endRegisterTime
  if (lastLoginTime && lastLoginTime.length > 0) {
    startLastLoginTime = lastLoginTime[0]
    endLastLoginTime = lastLoginTime[1]
  }
  if (registerTime && registerTime.length > 0) {
    startRegisterTime = registerTime[0]
    endRegisterTime = registerTime[1]
  }
  currentPage.value = 1
  searchParams.value = {
    startLastLoginTime,
    endLastLoginTime,
    startRegisterTime,
    endRegisterTime,
    ...rest
  }
  getList()
}
</script>

<template>
  <div class="flex w-100% h-100%">
    <ContentWrap class="flex-[3] ml-20px">
      <Search
        :schema="allSchemas.searchSchema"
        @reset="setSearchParams"
        @search="setSearchParams"
      />
      <Table
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :columns="allSchemas.tableColumns"
        :data="dataList"
        :loading="loading"
        @register="tableRegister"
        :pagination="{
          total
        }"
      />
    </ContentWrap>
  </div>
</template>
